package com.estate.charge.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.estate.bean.ChargeItem;
import com.estate.charge.service.ChargeItemService;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/chargeItem")
@Slf4j
public class ChargeItemController {
    @Resource
    private ChargeItemService chargeItemService;

    @GetMapping("list/{page}/{size}")
    public JsonResult list(@PathVariable("page") int page, @PathVariable("size") int size) {
        log.info("page:{},size:{}", page, size);
        JsonResult result=chargeItemService.listByPage(page, size);
        log.info("result:{}", result);
        return result;
    }
    @PostMapping("save")
    public JsonResult save(@RequestBody ChargeItem chargeItem) {
        log.info("chargeItem:{}", chargeItem);
        JsonResult result=chargeItemService.saveChargeItem(chargeItem);
        log.info("result:{}", result);
        return result;
    }
    @PutMapping("update")
    public JsonResult update(@RequestBody ChargeItem chargeItem) {
        log.info("chargeItem:{}", chargeItem);
        JsonResult result=chargeItemService.updateChargeItem(chargeItem);
        log.info("result:{}", result);
        return result;
    }
    @DeleteMapping("/delete")
    public JsonResult delete(@RequestParam("chargeItemId") String chargeItemId) {
        log.info("chargeItemId:{}", chargeItemId);
        JsonResult result=chargeItemService.deleteChargeItem(chargeItemId);
        log.info("result:{}", result);
        return result;
    }
    @GetMapping("/list/{chargeItemId}")
    public JsonResult list(@PathVariable("chargeItemId") String chargeItemId) {
        log.info("chargeItemId:{}", chargeItemId);
        JsonResult result= ResultTool.success(chargeItemService.getById(chargeItemId));
        log.info("result:{}", result);
        return result;
    }

    @GetMapping("/findByCommunityId/{communityId}")
    public Long findByCommunityId(@PathVariable("communityId") String communityId) {
        log.info("communityId:{}", communityId);
        QueryWrapper<ChargeItem> wrapper = new QueryWrapper<>();
        wrapper.eq("community_id", communityId);
        long count = chargeItemService.count(wrapper);
        log.info("count:{}", count);
        return count;
    }

}
